using System;
using System.IO;

namespace FileCopyTool
{
    /// <summary>
    /// 路径处理辅助类，兼容.NET Framework 4.7.2
    /// </summary>
    public static class PathUtils
    {
        /// <summary>
        /// 计算从相对路径到目标路径的相对路径
        /// </summary>
        public static string GetRelativePath(string relativeTo, string path)
        {
            if (string.IsNullOrEmpty(relativeTo)) 
                throw new ArgumentNullException(nameof(relativeTo), "基准路径不能为空");
            
            if (string.IsNullOrEmpty(path)) 
                throw new ArgumentNullException(nameof(path), "目标路径不能为空");

            string fullRelativeTo = Path.GetFullPath(relativeTo);
            string fullPath = Path.GetFullPath(path);

            // 处理不同驱动器的情况
            if (Path.GetPathRoot(fullRelativeTo) != Path.GetPathRoot(fullPath))
                return fullPath;

            // 处理根目录情况
            if (fullRelativeTo.EndsWith(Path.DirectorySeparatorChar.ToString()))
                fullRelativeTo = fullRelativeTo.TrimEnd(Path.DirectorySeparatorChar);
            fullRelativeTo += Path.DirectorySeparatorChar;

            Uri fromUri = new Uri(fullRelativeTo);
            Uri toUri = new Uri(fullPath);

            Uri relativeUri = fromUri.MakeRelativeUri(toUri);
            string relativePath = Uri.UnescapeDataString(relativeUri.ToString())
                .Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);

            return relativePath;
        }
    }
}
    